package api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.cfg.WxCfg;
import wxPaySdk.WXPay;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2018/12/2.
 */
@Service
public class WxTransfersApi {
    @Autowired
    WXPay wxPay;
    /**
     * 微信转账，强行校验姓名
     * @param
     * @return
     */
    public Map<String,String> transfers(String orderCode,String amount,String name,String openId,String desc) throws Exception {
        Map map = new HashMap();
        map.put("check_name", "FORCE_CHECK");
        map.put("partner_trade_no", orderCode);
        map.put("amount", amount);
        map.put("spbill_create_ip", getIp());
        map.put("re_user_name",name);
        map.put("openid", openId);
        map.put("desc",desc);
        return wxPay.transfers(map);
    }

    /**
     * 微信转账，不校验姓名
     * @param
     * @return
     */
    public Map<String,String> transfers(String orderCode,String amount,String openId,String desc) throws Exception {
        Map map = new HashMap();
        map.put("check_name", "NO_CHECK");
        map.put("partner_trade_no", orderCode);
        map.put("amount", amount);
        map.put("spbill_create_ip", getIp());
        map.put("openid", openId);
        map.put("desc",desc);
        return wxPay.transfers(map);
    }

    public String getIp() {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return "127.0.0.1";
        }
        String ip = addr.getHostAddress().toString();
        return ip;
    }
}
